package com.lunwen.view;

import com.lunwen.dao.ArticleDao;
import com.lunwen.domain.Article;
import com.lunwen.view.component.Context;
import com.lunwen.view.component.MassageTip;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArticleFrame extends Context implements ActionListener {

    private JTextField one = new JTextField("无");
    private JTextField two = new JTextField("无");
    private JTextField three = new JTextField("无");
    private JTextField four = new JTextField("无");
    private JTextField five = new JTextField("无");
    private JTextField sex = new JTextField("无");
    private JTextField seven = new JTextField("无");
    private JTextField eight = new JTextField("无");
    private JTextField night = new JTextField("无");

    private JButton left = new JButton("<");
    private JButton right = new JButton(">");

    private ArticleDao dao = new ArticleDao();

    private List<Article> cache;
    private int indexOfCache;
    ArrayList<JTextField> set = new ArrayList<JTextField>();

    public ArticleFrame(int width, int height) {
        super(width, height);
        one.setBounds(60, 0, width-120, 40);
        two.setBounds(60, one.getHeight()+one.getLocation().y+10, width-120, 40);
        three.setBounds(60, two.getHeight()+two.getLocation().y+10, width-120, 40);
        four.setBounds(60, three.getHeight()+three.getLocation().y+10, width-120, 40);
        five.setBounds(60, four.getHeight()+four.getLocation().y+10, width-120, 40);
        sex.setBounds(60, five.getHeight()+five.getLocation().y+10, width-120, 40);
        seven.setBounds(60, sex.getHeight()+sex.getLocation().y+10, width-120, 40);
        eight.setBounds(60, seven.getHeight()+seven.getLocation().y+10, width-120, 40);
        night.setBounds(60, eight.getHeight()+eight.getLocation().y+10, width-120, 40);
        this.add(one,"",10);one.setEditable(false);
        this.add(two, "", 10);two.setEditable(false);
        this.add(three, "", 10);three.setEditable(false);
        this.add(four, "", 10);four.setEditable(false);
        this.add(five, "", 10);five.setEditable(false);
        this.add(sex, "", 10);sex.setEditable(false);
        this.add(seven, "", 10);seven.setEditable(false);
        this.add(eight, "", 10);eight.setEditable(false);
        this.add(night, "", 10);night.setEditable(false);
        set.add(one);set.add(two);set.add(three);set.add(four);set.add(five);set.add(sex);
        set.add(seven);set.add(eight);set.add(night);

        left.setBounds(0,(height-40)/2,40,40);
        this.add(left,20);
        right.setBounds(width-40,(height-40)/2,40,40);
        this.add(right,20);

        left.addActionListener(this);
        right.addActionListener(this);

        List<Article> articles = dao.findAll();
        cache = articles;
        indexOfCache=0;
        System.out.println(articles);
        if (articles.size() > 9){
            for (int i = 0; i < 9; i++) {
                set.get(i).setText((i+1)+". "+articles.get(i).getName());
            }
        }else {
            for (int i = 0; i < articles.size(); i++) {
                set.get(i).setText((i+1)+". "+articles.get(i).getName());
            }
        }

        List<JTextField> list = new ArrayList<>(Arrays.asList(one,two,three,four,five,sex,seven,eight,night));
        for (int i = 0; i < list.size(); i++) {
            int tempi = i;
            list.get(i).addMouseListener(new MouseListener(){
                @Override
                public void mouseClicked(MouseEvent e) {
                    //获取论文内容
                    String content = dao.findByAid(cache.get((indexOfCache + tempi)).getAid());
                    JTextArea jTextArea = new JTextArea(20,40);
                    jTextArea.setLineWrap(true);
                    jTextArea.setEditable(true);
                    jTextArea.setText(content);

                    JScrollPane jScrollPane = new JScrollPane(jTextArea);
                    jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

                    JDialog jDialog = new JDialog();
                    Container contentPane = jDialog.getContentPane();
                    //设置论文标题
                    jDialog.setTitle(cache.get((indexOfCache + tempi)).getName());
                    jDialog.setSize(400,500);
                    jDialog.setLocation(850,300);
                    jDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

                    contentPane.add(jScrollPane);
                    jDialog.setVisible(true);
                }

                @Override
                public void mousePressed(MouseEvent e) {

                }

                @Override
                public void mouseReleased(MouseEvent e) {

                }

                @Override
                public void mouseEntered(MouseEvent e) {

                }

                @Override
                public void mouseExited(MouseEvent e) {

                }
            });
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (cache != null){
            if (e.getSource() == left){
                if (indexOfCache > 0){
                    indexOfCache-=9;
                    setValue(cache);
                }else{
                    MassageTip.showMassage(null,"前面再没有了");
                }
            }else if (e.getSource() == right){
                if (indexOfCache < cache.size()-1){
                    indexOfCache+=9;
                    setValue(cache);
                }else {
                    MassageTip.showMassage(null,"后面再没有了");
                }
            }
        }
    }

    private void setValue(List<Article> cache) {
        for (int i = 0; i < 9; i++) {
            if (cache.size()>indexOfCache+i)
                set.get(i).setText((indexOfCache+i+1)+". "+cache.get(indexOfCache+i).getName());
            else set.get(i).setText("");
        }
    }


}
